
package com.yh.correct.controller;

import java.util.*;

import com.yh.correct.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.yh.correct.model.ResultData;
import com.yh.correct.model.SystemMailbox;
import com.yh.correct.service.ISystemMailboxService;
import org.springframework.web.servlet.ModelAndView;

/**
 * 信息站管理控制层
 *
 * @author 崔赛赛
 */
@RestController
@RequestMapping("/points")
public class SystemMailboxController {
    //实例化信站箱服务层接口
    @Autowired
    private ISystemMailboxService iSystemMailboxService;

    /**
     * 跳转到发件箱页面
     *
     * @return
     */
    @RequestMapping(value = "/show1")
    public ModelAndView skip() {
        ModelAndView modelAndView = new ModelAndView("/message/mail_send");
        return modelAndView;
    }

    /**
     * 跳转到收件箱页面
     *
     * @return
     */
    @RequestMapping(value = "/show2")
    public ModelAndView skip2() {
        ModelAndView modelAndView = new ModelAndView("/message/mail_receive");
        return modelAndView;
    }

    /**
     * 根据id添加信件
     */

    @RequestMapping("/add")
    public ResultData add(SystemMailbox systemMailbox) {

        SystemMailbox ss = new SystemMailbox();
        ss.setTitle("信站箱管理");
        ss.setContent("接到最新通知!");
        ss.setSendTime(new Date());
        ss.setStatus(0);
        ss.setSendId(01);
        ss.setReceiveId(0);
        ResultData rd = new ResultData();
        List<SystemMailbox> mm = new ArrayList<SystemMailbox>();
        mm.add(ss);
        int i = iSystemMailboxService.addSystemMailbox(systemMailbox);
        if (i > 0) {
            rd.setCode(0);
            rd.setMessage("插入成功！");
            rd.setData(mm);
        } else {

            rd.setCode(-1);
            rd.setMessage("插入0！");

        }
        return rd;
    }

    /**
     * 根据id删除信件
     */
    @RequestMapping("/del")
    public ResultData del(int id) {
        ResultData rd = new ResultData();
        int res = iSystemMailboxService.delSystemMailbox(id);
        if (res > 0) {
            rd.setCode(0);
            rd.setMessage("删除成功！");
        } else {
            rd.setCode(-1);
            rd.setMessage("删除失败！");
        }
        return rd;
    }

    /**
     * 修改信件
     */
    @RequestMapping("/edit")
    public ResultData edit(SystemMailbox systemMailbox) {
        SystemMailbox ss = new SystemMailbox();

        ss.setTitle("信站箱管理01");
        ss.setContent("接到最新通知!2222");
        ss.setSendTime(new Date());
        ss.setStatus(0);
        ss.setSendId(01);
        ss.setReceiveId(0);
        ss.setId(2);
        ResultData rd = new ResultData();
        List<SystemMailbox> cc = new ArrayList<SystemMailbox>();
        cc.add(ss);
        iSystemMailboxService.editSystemMailbox(ss);
        rd.setCode(0);
        rd.setMessage("修改成功");
        rd.setData(cc);
        return rd;
    }

    /**
     * 修改信件主题、内容、收件人
     *
     * @param systemMailbox
     * @return
     */
    @RequestMapping("/edits")
    public ResultData editMail(SystemMailbox systemMailbox) {
        ResultData rd = new ResultData();
        List<SystemMailbox> list = new ArrayList<SystemMailbox>();
        list.add(systemMailbox);
        int res = iSystemMailboxService.editMail(systemMailbox);
        if (res > 0) {
            rd.setCode(0);
            rd.setMessage("修改成功");
            rd.setData(list);
        } else {
            rd.setCode(-1);
            rd.setMessage("修改失败");
            rd.setData(list);
        }
        return rd;
    }

    /**
     * 根据id信件
     */
    @RequestMapping("/get")
    public ResultData get(SystemMailbox systemMailbox) {
        SystemMailbox ss = new SystemMailbox();
        ResultData rd = new ResultData();
        List<SystemMailbox> mm = new ArrayList<SystemMailbox>();
        Integer i = 2;
        SystemMailbox a = iSystemMailboxService.getSystemMailbox(i);
        mm.add(a);
        rd.setCode(0);
        rd.setMessage("查看成功！");
        rd.setData(mm);
        return rd;

    }

    /**
     * 模糊查询发件箱信息
     *
     * @param sendId
     * @param content
     * @param startTime
     * @param endTime
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("getSendMessage")
    public ResultData getSendIdMessage(String sendId, String content, String startTime, String endTime, Integer pageNum, Integer pageSize) throws Exception {
        Map map = new HashMap<>();
        map.put("sendId", sendId);
        map.put("content", content);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        Page page = iSystemMailboxService.getSendIdMessage(map);
        ResultData rd = new ResultData();
        if (page != null) {
            List<Object> lp = new ArrayList();
            lp.add(page);
            rd.setCode(0);
            rd.setMessage("查询成功");
            rd.setData(lp);
        } else {
            rd.setCode(-1);
            rd.setMessage("查询失败");
        }
        return rd;
    }

    /**
     * 根据id查询收件箱未读信息条数
     *
     * @return
     */
    @RequestMapping("getCount")
    public ResultData getCount(String receiveId) throws Exception {
        int count = iSystemMailboxService.getUnReadMessageCount(Integer.valueOf(receiveId));
        ResultData rd = new ResultData();
        if (count > 0) {
            List<Object> list = new ArrayList<>();
            list.add(count);
            rd.setCode(0);
            rd.setMessage("查询成功");
            rd.setData(list);
        } else {
            rd.setCode(-1);
            rd.setMessage("查询失败");
        }
        return rd;
    }

    /**
     * 模糊查询收件箱信息
     *
     * @param receiveId
     * @param content
     * @param startTime
     * @param endTime
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("getReceiveMessage")
    public ResultData getReceiveMessage(String receiveId, String content, String startTime, String endTime, Integer pageNum, Integer pageSize) throws Exception {
        Map map = new HashMap<>();
        map.put("receiveId", receiveId);
        map.put("content", content);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        Page page = iSystemMailboxService.getReceiveIdMessage(map);
        ResultData rd = new ResultData();
        if (page != null) {
            List<Object> lp = new ArrayList();
            lp.add(page);
            rd.setCode(0);
            rd.setMessage("查询成功");
            rd.setData(lp);
        } else {
            rd.setCode(-1);
            rd.setMessage("查询失败");
        }
        return rd;
    }

    /**
     * 查询所有信件
     */
    @RequestMapping("/getAll")
    public ResultData getAll(SystemMailbox systemMailbox) {
        List<SystemMailbox> o = iSystemMailboxService.getAllSystemMailbox();
        ResultData rd = new ResultData();

        rd.setCode(0);
        rd.setMessage("查询所有信息成功！");
        rd.setData(o);
        return rd;
    }

    /**
     * 跳转到增加信息页面
     *
     * @return
     */
    @RequestMapping("addskip")
    public ModelAndView skip1() {
        return new ModelAndView("/message/mailcontent_add");
    }

    /**
     * 跳转到查看信息内容页面
     *
     * @param content
     * @return
     */
    @RequestMapping("lookcontent")
    public ModelAndView skip2(String rd, String id, String content) {
        Map map = new HashMap<>();
        map.put("id", id);
        //rd=1时，需要把信息状态设为已读，rd=0时，仅仅是查看一下信息
        map.put("rd", rd);
        map.put("content", content);
        return new ModelAndView("/message/mailcontent_look", map);
    }

    /**
     * 跳转到修改信息内容页面
     *
     * @param mid
     * @param mcontent
     * @return
     */
    @RequestMapping("skip")
    public ModelAndView skip3(String mid, String mtitle, String mcontent, String mreceivename) {
        Map map = new HashMap<>();
        map.put("id", mid);
        map.put("title", mtitle);
        map.put("content", mcontent);
        map.put("receive", mreceivename);
        return new ModelAndView("/message/mailcontent_edit", map);
    }

    /**
     * 保存信件
     *
     * @param systemMailbox
     * @return
     */
    @RequestMapping("savemail")
    public ResultData saveMail(SystemMailbox systemMailbox) {
        int i = 0;
        try {
            i = iSystemMailboxService.saveMail(systemMailbox);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResultData rd = new ResultData();
        if (i > 0) {
            rd.setCode(0);
            rd.setMessage("增加成功！");
        } else {
            rd.setCode(-1);
            rd.setMessage("增加失败！");
        }
        return rd;
    }

    /**
     * 发送信件
     *
     * @param systemMailbox
     * @return
     */
    @RequestMapping("pubmail")
    public ResultData pubMail(SystemMailbox systemMailbox) {
        int i = 0;

        try {
            i = iSystemMailboxService.pubMail(systemMailbox);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResultData rd = new ResultData();
        if (i > 0) {
            rd.setCode(0);
            rd.setMessage("发送成功！");
        } else {
            rd.setCode(-1);
            rd.setMessage("发送失败！");
        }
        return rd;
    }

    /**
     * 通过修改发送状态发送信件
     *
     * @param id
     * @return
     */
    @RequestMapping("pubmails")
    public ResultData pubMails(int id) {
        int i = 0;
        try {
            i = iSystemMailboxService.pubMails(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResultData rd = new ResultData();
        if (i > 0) {
            rd.setCode(0);
            rd.setMessage("发送成功！");
        } else {
            rd.setCode(-1);
            rd.setMessage("发送失败！");
        }
        return rd;
    }

    /**
     * 读取信息
     *
     * @param id
     * @return
     */
    @RequestMapping("readMessage")
    public ResultData readMessage(int id) {
        int i = 0;
        try {
            i = iSystemMailboxService.readMessage(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResultData rd = new ResultData();
        if (i > 0) {
            rd.setCode(0);
            rd.setMessage("读取成功！");
        } else {
            rd.setCode(-1);
            rd.setMessage("读取失败！");
        }
        return rd;
    }

    /**
     * 批量删除信件
     */
    @RequestMapping("/deleteBatch")
    public ResultData deleteBatch(@RequestParam("ids[]") List<Integer> list) {
        ResultData rd = new ResultData();
        int i = 0;
        try {
            i = iSystemMailboxService.deleteBatch(list);
            if (i > 0) {
                rd.setCode(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rd;
    }
}


